Skip to content

Conversation

@Stephen-Allen
Copy link

@Stephen-Allen Stephen-Allen commented Sep 24, 2024

When using a native query on an entity with single table inheritance, hibernate does not set/discover the column positions for reading the result set. This is a regression from 5.6.15 to 6.x.

I've attached a test case that demonstrates the issue. Note that it may appear to work using a database with a lenient JDBC driver (H2/HSQL) because it will allow you to specify the table column name instead of the alias when retrieving results. However with a strict JDBC driver (such as PostgreSQL) it will fail since only the alias names are available in the JDBC metadata. Even though the lenient driver appears to work, it is incorrect when a property on two subclasses have the same name but different column names.

This first commit fixes the issue for the discriminator column name, but there is a similar issue with the subclass column names not working that I was not able to address. The main issue with fixing that is that it appears that we need to add those subclass properties to the sqlSelectionMap in DomainResultCreationStateImpl via the ResultSetMappingProcessor.createSuffixedResultBuilder(EntityMappingType, String, String,LockMode, NavigablePath) method like we do with the base class property names (ResultSetMappingProcessor.java line 325). However I'm not quite sure how to do this because of the fact that that map uses a ColumnReferenceKey as a key which only consists of the tableQualifier, selectablePath, and jdbcMapping. I don’t think that will work for subclasses that have properties with the same name but different column names.

Because of this remaining issue this PR is not complete and the itShouldGetPersons and itShouldGetWife tests do not pass yet with the matrix_pg (and probably other databases).


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license
and can be relicensed under the terms of the LGPL v2.1 license in the future at the maintainers' discretion.
For more information on licensing, please check here.


https://hibernate.atlassian.net/browse/HHH-18610

for ( int k = 0; k < subclasses.size(); k++ ) {
Subclass subclass = subclasses.get( k );
subclassClosure[k] = subclass.getEntityName();
subclassClosure[k+1] = subclass.getEntityName();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This off by one error was accidentally introduced in commit a7da407 during a refactor from a foreach loop to a for loop.


// SUBCLASSES
final List<Subclass> subclasses = persistentClass.getSubclasses();
final List<Subclass> subclasses = persistentClass.getSubclasses().stream().sorted(Comparator.comparing(PersistentClass::getEntityName)).collect(Collectors.toList());
Copy link
Author

@Stephen-Allen Stephen-Allen Sep 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The subclasses need to be ordered by the entity name to match the TreeMap with entity name keys used for AbstractEntityPersister.subclassMappingTypes. Otherwise the column orders can get switched up incorrectly and return the wrong values.

final JdbcMapping jdbcMapping = (selectable instanceof DiscriminatorMapping) ?
((DiscriminatorMapping)selectable).getUnderlyingJdbcMapping() :
selectable.getJdbcMapping();
return createColumnReferenceKey( tableReference, selectable.getSelectablePath(), jdbcMapping );
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes the discriminator case by using the underlying JDBC type as part of the key to look up the discriminator property in the sqlSelectionMap in DomainResultCreationStateImpl.resolveSqlExpression(ColumnReferenceKey key, Function<SqlAstProcessingState, Expression> creator) function.

@beikov
Copy link
Member

beikov commented Nov 29, 2024

Closing since this was fixed via #9291 already.

@beikov beikov closed this Nov 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants